
import { AppContext } from "../app.context";
import { AlbumAuthority } from "../mode/album";
import { BaseServices } from "./base.services";
import { APIMethod, APIUrl } from "./parameters";


export class AlbumService extends BaseServices {
  private static _instance: AlbumService;
  public static get instance(): AlbumService {
    if (this._instance == null) {
      this._instance = new AlbumService();
    }
    return this._instance;
  }
  constructor() { super() }

  /** 获取所有相册 */
  getList() {
    const list = AppContext.instance.resources.album;
    console.log(list);

    if (list.length > 0) {
      return this.returnImmediately();
    }
    const owner = AppContext.instance.user.data.uid;
    if (!owner) {
      return this.returnImmediately();
    }
    const url = APIUrl.PhotoAlbum + APIMethod.List;
    const data = { owner };
    return this.request<any>(url, data).then(res => {
      console.log(res);
      AppContext.instance.resources.setAlbum(res);
      return res;
    })
  }
  /** 创建相册 */
  createAlbum(name: string, remark: string, isPublic?: AlbumAuthority, cover?: string, tag?: string[]) {
    const owner = AppContext.instance.user.data.uid;

    const url = APIUrl.PhotoAlbum + APIMethod.Create;
    const data = { name, owner, authority: isPublic, cover, remark, tag };
    return this.request<any>(url, data).then(res => {
      console.log(res);
      AppContext.instance.resources.addAlbum(res);
    })
  }

  /**
   * 相册中添加图片资源
   * @param uid 
   * @param keys 
   * @returns 
   */
  addAssets(uid: string, assets: string[]) {
    const url = APIUrl.PhotoAlbum + APIMethod.AddAssets;
    const data = { uid, assets };
    return this.request<any>(url, data).then(res => {
      console.log(res);
      // AppContext.instance.statisResources.album.addAlnumAssets(uid, res);
    })
  }

  cleanEmpty(uid: string) {
    const url = APIUrl.PhotoAlbum + APIMethod.CleanEmpty;
    const data = { uid };
    return this.request<any>(url, data).then(res => {
      console.log(res);
      // AppContext.instance.statisResources.album.addAlnumAssets(uid, res);
    })
  }

  /**
   * 关联七牛云资源
   * @param name 
   * @param small 
   * @param remark 
   * @returns 
   */
  uploadAsset(name: string, originalDrawing: string, small?: string) {
    const owner = AppContext.instance.user.data.uid;
    const url = APIUrl.ImageAsset + APIMethod.Create;
    const data = { name, owner, small, url: originalDrawing };
    return this.request<any>(url, data).then(res => {
      console.log(res);
      return res;
      // AppContext.instance.statisResources.album.createAlbum(res);
    })
  }
}